﻿using EyFramework.Common;
using RestSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ShuJuGj.Core
{
   
    public static class Interface
    {

        /// <summary>
        /// 获取Token值
        /// </summary>
        /// <param name="url">网址</param>
        /// <param name="body">传输信息</param>
        /// <returns></returns>
        public static string PostGetToken(String url, String body)
        {
            var client = new RestClient(url);
            client.Timeout = -1;
            var request = new RestRequest(Method.POST);
            request.AddHeader("Content-Type", "text/plain");
            request.AddParameter("text/plain", body, ParameterType.RequestBody);
            IRestResponse response = client.Execute(request);
            return response.Content;
        }

        /// <summary>
        /// 获取Token值
        /// </summary>
        /// <param name="url">网址</param>
        /// <param name="body">传输信息</param>
        /// <returns></returns>
        public static string PostGetToken_Tx(String url, String body)
        {
            var client = new RestClient(url);
            client.Timeout = -1;
            var request = new RestRequest(Method.POST);
            request.AddHeader("Content-Type", "text/plain");
            request.AddHeader("authoritytype", ShuJuGjConstants.TXHead_authoritytype);
            request.AddHeader("license", ShuJuGjConstants.TXHead_license);
            request.AddParameter("text/plain", body, ParameterType.RequestBody);
            IRestResponse response = client.Execute(request);
            return response.Content;
        }

        /// <summary>
        /// 向浙里办发送数据
        /// </summary>
        /// <param name="url">网址</param>
        /// <param name="token">头文件</param>
        /// <param name="body">传输信息</param>
        /// <returns></returns>
        public static string PostGet(String url, String token, string body)
        {
            var client = new RestClient(url);
            client.Timeout = -1;
            var request = new RestRequest(Method.POST);
            request.AddHeader("ACCESS-TOKEN", token);
            //var body = @"";
            request.AddParameter("text/plain", body, ParameterType.RequestBody);
            IRestResponse response = client.Execute(request);
            return response.Content;
        }

        /// <summary>
        /// 向浙里办发送数据
        /// </summary>
        /// <param name="url">网址</param>
        /// <param name="token">头文件</param>
        /// <param name="body">传输信息</param>
        /// <returns></returns>
        public static string Tx_PostGet(String url, String token, string body)
        {
            var client = new RestClient(url);
            client.Timeout = -1;
            var request = new RestRequest(Method.POST);
            request.AddHeader("ACCESS-TOKEN", token);
            request.AddHeader("authoritytype", ShuJuGjConstants.TXHead_authoritytype);
            request.AddHeader("license", ShuJuGjConstants.TXHead_license);
            //var body = @"";
            request.AddParameter("text/plain", body, ParameterType.RequestBody);
            IRestResponse response = client.Execute(request);
            return response.Content;
        }

        /// <summary>
        /// 补齐【】获得token
        /// </summary>
        /// <param name="str">未处理的数据</param>
        /// <returns></returns>
        public static String ChangeJson(string str)
        {
            string json = "";
            int start = 0;
            int end = 0;
            start = str.Substring(str.IndexOf("{") + 1, str.Length - str.IndexOf("{") - 1).IndexOf("{") + str.IndexOf("{") + 1;
            end = str.IndexOf("}");
            if (start == 0)
            {
                return json;
            }
            else
            {
                return str.Substring(0, start) + "[" + str.Substring(start, end - start + 1) + "]" + str.Substring(end + 1, str.Length - end - 1);
            }

        }

        /// <summary>
        /// 验证返回数据
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static String Test_data(String str)
        {
            String result = "";
            if (str.IndexOf("status") == -1)
            {
                result = AESHelper.AESDecrypt(str, ShuJuGjConstants.TXTJYY_AESKEY);
            }
            else
            {
                str = str.Substring(str.IndexOf("[") + 1, str.IndexOf("]") - str.IndexOf("[") - 1);
                result = AESHelper.AESDecrypt(str, ShuJuGjConstants.TXTJYY_AESKEY);
            }
            return result;
        }
    }
}
